import logging
import os
import time


class Logger():
    def __init__(self,module_name):
        #  格式器
        fmt = '%(asctime)s %(name)s [%(levelname)s] %(message)s '
        datefmt = '%Y-%m-%d'
        format = logging.Formatter(fmt=fmt, datefmt=datefmt)

        # 处理器：StreamHandler---控制台输出方案  FileHandler：文件输出方案
        sh = logging.StreamHandler()
        sh.setLevel(logging.INFO)
        sh.setFormatter(format)

        times = time.strftime('%Y-%m-%d')
        log_path = os.path.dirname(os.path.realpath(__file__)) + '\\..\\logs\\'
        #print(log_path)
        log_name = log_path + times + '.log'
        print(module_name)
        fh = logging.FileHandler(log_name, encoding='utf-8')
        fh.setLevel(logging.INFO)
        fh.setFormatter(format)

        # 日志器：定义级别和添加上处理器
        # 他和basicConfig不同，他是用类的方式来处理，而basicConfig是函数形式
        self.logger = logging.getLogger(module_name)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(sh)
        self.logger.addHandler(fh)


    def getlogger(self):
       return self.logger


# if __name__ == '__main__':
#     # study_logging().info('我叫蔡昶')
#     logger = Logger()
#     #logger.study_logging().info('我叫蔡昶')
#
#     print(logger.getlogger().info('我叫蔡昶'))

